package com.blunka.framework.client.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

import org.apache.commons.lang3.StringUtils;
import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;
import org.jdesktop.swingx.treetable.TreeTableNode;

import com.blunka.framework.client.enums.ModelType;
import com.blunka.framework.client.ui.entity.BlkTableMetaData;

/**
 * 节点属性窗口
 * @author Administrator
 *
 */
@SuppressWarnings("serial")
public class AttrPanel extends JPanel{

	public static AttrPanel attrPanel = new AttrPanel();

	private JXTreeTable attrTable;

	public static AttrPanel newInstance(){
		return attrPanel;
	}

	private AttrPanel(){
		this.setLayout(new BorderLayout());
		attrTable = new JXTreeTable(new AttrModel(initAttrInfo()));
		this.add(new JScrollPane(attrTable),BorderLayout.CENTER );
		attrTable.getColumnModel().getColumn(0).setPreferredWidth(150); //  设置第一列宽度
		attrTable.setRootVisible(true); //  显示根结点
		attrTable.expandAll();  //  展开全部节点
		attrTable.setRowHeight(25);
		//attrTable.setFont(new Font("宋体",1,14));
		//attrTable.getTableHeader().setFont(new Font("宋体",1,14));
		attrTable.getTableHeader().setForeground(Color.BLUE);


		TableColumn fCol = attrTable.getColumnModel().getColumn(0);
		fCol.setPreferredWidth(200);
		fCol.setMaxWidth(200);
		fCol.setMinWidth(100);
		//设置第一列属性
		fCol.setCellRenderer(new DefaultTableCellRenderer(){
	        public void setValue(Object value) {
	            //setForeground(Color.lightGray);
	            //setFont(new java.awt.Font("宋体", 1, 14));
	            setText((value == null) ? "" : value.toString());
	        }
		});

		//设置第二列属性
		TableColumn sCol = attrTable.getColumnModel().getColumn(1);
		sCol.setCellRenderer(new DefaultTableCellRenderer(){
	        public void setValue(Object value) {
	            //setForeground(Color.lightGray);
	            //setFont(new java.awt.Font("宋体", 1, 14));
	            setText((value == null) ? "" : value.toString());
	        }
		});

		//关闭字段节点属性
		attrTable.collapseRow(8);
	}

	/**
	 * 初始化属性面板信息
	 * @return
	 */
	private DefaultMutableTreeTableNode initAttrInfo(){
		Map<String,Object> nodeMap =  new HashMap<String,Object>();
		nodeMap.put("k", "节点属性");
        DefaultMutableTreeTableNode nodeRoot = new DefaultMutableTreeTableNode(nodeMap);


        Map<String,Object> childMap =  new HashMap<String,Object>();
        childMap.put("k", "节点类型");
        nodeRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "中文名");
        nodeRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "英文名");
        nodeRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "描述");
        nodeRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "路径");
        nodeRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "状态");
        nodeRoot.add(new DefaultMutableTreeTableNode(childMap));



        Map<String,Object> rootMap = new HashMap<String,Object>();
        rootMap.put("k", "建模属性");
        DefaultMutableTreeTableNode root = new DefaultMutableTreeTableNode(rootMap);
        root.add(nodeRoot);
        initFieldAttr(root);
        return root;
	}

	/**
	 * 生成字段属性
	 * @param rootNode
	 */
	public void initFieldAttr(DefaultMutableTreeTableNode rootNode){
        Map<String,Object> childMap =  new HashMap<String,Object>();
        childMap.put("k", "字段属性");
        DefaultMutableTreeTableNode fieldRoot = new DefaultMutableTreeTableNode(childMap);
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "字段类型");
        fieldRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "字段长度");
        fieldRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "主键");
        fieldRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "是否可为空");
        fieldRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "显示长度");
        fieldRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "是否隐藏");
        fieldRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "是否可预览");
        fieldRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "是否只读");
        fieldRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "是否可编辑");
        fieldRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "错误提示");
        fieldRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "输入提示");
        fieldRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "所属分组信息");
        fieldRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "自定义配置属性");
        fieldRoot.add(new DefaultMutableTreeTableNode(childMap));
        childMap =  new HashMap<String,Object>();
        childMap.put("k", "下拉字段属性配置值");
        fieldRoot.add(new DefaultMutableTreeTableNode(childMap));


        rootNode.add(fieldRoot);
	}

	/**
	 * 关闭字段属性
	 */
	public void collapseField(){
		attrTable.collapseRow(8);
	}

	/**
	 * 打开字段属性
	 */
	public void expandField(){
		attrTable.expandRow(8);
	}


	/**
	 * 刷新树节点属性数据
	 * @param node
	 */
	@SuppressWarnings("unchecked")
	public void refreshTableData(BlkTableMetaData model){
		TreeTableNode rootNode = (TreeTableNode)attrTable.getTreeTableModel().getRoot();
		//根据类型打开或关闭字段节点
		if(model.getModel_type() == ModelType.ATTRIBUTE){
			expandField();
		}else{
			collapseField();
		}

		//节点属性根
		TreeTableNode nodeRoot = rootNode.getChildAt(0);
		//字段属性根
		TreeTableNode fieldRoot = rootNode.getChildAt(1);
		for(int i=0;i<=attrTable.getRowCount();i++){
			switch(i){
				case 0:{
					break;
				}
				case 1:{
					break;
				}
				case 2:{
					//节点类型
					TreeTableNode node = nodeRoot.getChildAt(0);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					obj.put("v", model.getModel_type().getName());
					node.setUserObject(obj);
					break;
				}
				case 3:{
					//中文名称
					TreeTableNode node = nodeRoot.getChildAt(1);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					obj.put("v", model.getZh_cn());
					node.setUserObject(obj);
					break;
				}
				case 4:{
					//英文名称
					TreeTableNode node = nodeRoot.getChildAt(2);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					obj.put("v", model.getEn_us());
					node.setUserObject(obj);
					break;
				}
				case 5:{
					//节点描述
					TreeTableNode node = nodeRoot.getChildAt(3);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					obj.put("v", model.getDescribes());
					node.setUserObject(obj);
					break;
				}
				case 6:{
					//节点路径
					TreeTableNode node = nodeRoot.getChildAt(4);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					obj.put("v", model.getObj_path());
					node.setUserObject(obj);
					break;
				}
				case 7:{
					//状态
					TreeTableNode node = nodeRoot.getChildAt(5);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					obj.put("v", model.getModel_states().getName());
					node.setUserObject(obj);
					break;
				}
				case 8:{
					break;
				}
				case 9:{
					//字段类型
					TreeTableNode node = fieldRoot.getChildAt(0);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					if(model.getModel_field() !=null){
						obj.put("v", model.getModel_field().getName());
					}else{
						obj.put("v",null);
					}
					node.setUserObject(obj);
					break;

				}
				case 10:{
					//字段长度
					TreeTableNode node = fieldRoot.getChildAt(1);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					obj.put("v", model.getModel_field_len());
					node.setUserObject(obj);
					break;

				}
				case 11:{
					//主键
					TreeTableNode node = fieldRoot.getChildAt(2);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					if(model.getIs_primary_key() !=null){
						obj.put("v", model.getIs_primary_key().getName());
					}else{
						obj.put("v",null);
					}
					node.setUserObject(obj);
					break;

				}
				case 12:{
					//是否为空
					TreeTableNode node = fieldRoot.getChildAt(3);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					if(model.getIs_empty() !=null){
						obj.put("v", model.getIs_empty().getName());
					}else{
						obj.put("v",null);
					}
					node.setUserObject(obj);
					break;

				}
				case 13:{
					//显示长度
					TreeTableNode node = fieldRoot.getChildAt(4);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					obj.put("v", model.getShow_width());
					node.setUserObject(obj);
					break;

				}
				case 14:{
					//是否隐藏
					TreeTableNode node = fieldRoot.getChildAt(5);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					if(model.getIs_hidden() !=null){
						obj.put("v", model.getIs_hidden().getName());
					}else{
						obj.put("v",null);
					}
					node.setUserObject(obj);
					break;

				}
				case 15:{
					//是否可预览
					TreeTableNode node = fieldRoot.getChildAt(6);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					if(model.getIs_viewer() !=null){
						obj.put("v", model.getIs_viewer().getName());
					}else{
						obj.put("v",null);
					}
					node.setUserObject(obj);
					break;

				}
				case 17:{
					//是否只读
					TreeTableNode node = fieldRoot.getChildAt(7);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					if(model.getIs_reader() !=null){
						obj.put("v", model.getIs_reader().getName());
					}else{
						obj.put("v",null);
					}
					node.setUserObject(obj);
					break;

				}
				case 18:{
					//是否可编辑
					TreeTableNode node = fieldRoot.getChildAt(8);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					if(model.getIs_disabled() !=null){
						obj.put("v", model.getIs_disabled().getName());
					}else{
						obj.put("v",null);
					}
					node.setUserObject(obj);
					break;

				}
				case 19:{
					//错误提示
					TreeTableNode node = fieldRoot.getChildAt(9);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					obj.put("v", model.getError_msg());
					node.setUserObject(obj);
					break;

				}
				case 20:{
					//输入提示
					TreeTableNode node = fieldRoot.getChildAt(10);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					obj.put("v", model.getInput_promit());
					node.setUserObject(obj);
					break;

				}
				case 21:{
					//所属分组信息
					TreeTableNode node = fieldRoot.getChildAt(11);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					obj.put("v", model.getGroup_title());
					node.setUserObject(obj);
					break;

				}
				case 22:{
					//自定义属性配置
					TreeTableNode node = fieldRoot.getChildAt(12);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					if(StringUtils.isEmpty(model.getUser_config())){
						obj.put("v", "自定义属性暂未配置");
					}else{
						//obj.put("v", model.getUserConfig());
						obj.put("v", "自定义属性已配置");
					}
					node.setUserObject(obj);
					break;
				}
				case 23:{
					//下拉字段属性配置
					TreeTableNode node = fieldRoot.getChildAt(13);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					if(StringUtils.isEmpty(model.getSel_options())){
						obj.put("v", "数据暂未配置");
					}else{
						//obj.put("v", model.getSelOptions());
						obj.put("v", "数据已配置");
					}

					node.setUserObject(obj);
					break;
				}
				/*case 24:{
					//试图语句配置
					TreeTableNode node = fieldRoot.getChildAt(14);
					Map<String,Object> obj = (Map<String,Object>)node.getUserObject();
					if(StringUtils.isEmpty(model.getSel_options())){
						obj.put("v", "数据暂未配置");
					}else{
						obj.put("v", "数据已配置");
					}

					node.setUserObject(obj);
					break;
				}*/
			}
		}
		attrTable.updateUI();
	}



}
